<?php

/*
 *
 *
 */
define('ABSPATH', dirname(__FILE__) . '/');
if (function_exists('xdebug_start_trace')) {
    //xdebug_start_trace(ABSPATH . 'trace.xt' . uniqid());
}
ob_implicit_flush(true);
require_once(dirname(__FILE__) . '/../include.all.php');

$qwe = createDeferredFunction(function($a, $b) {
    echo "process: $a $b\n";
    $result = new Deferred();
    setTimeout(function() use ($a, $b, $result) {
        $result->errback($a . $b);
    }, 1);
    return $result;
});
$res = $qwe('a', '');
$res = $qwe($res, 'b');
$res = $qwe($res, 'c');
$res = $qwe($res, 'd');
$res = $qwe($res, 'e');
$res->addCallback(function($data) {
    echo $data;
    return $data;
});
$res->addErrback(function($data) {
    echo 'err ' . $data;
    return $data;
});


/*
function qwe($data) {
    $result = new Deferred();
    if(!is_a($data, 'Deferred')) {
        $start = new Deferred();
        $start->callback($data);
    } else {
        $start = $data;
    }
    $start->addBoth(function($data) use ($result) {
        //start body
        setTimeout(function() use ($data, $result) {
            $last = substr($data, -1);
            $last++;
            $data .= $last;
            //return result
            $result->callback($data);
        }, 1);
        //end body
    });
    return $result;
}
*/



echo "\nAll Init\n";
Tick::getInstance()->startExecution();
echo "\nAll Ended\n";
